package com.yj.suncity.utis;

import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.yj.suncity.R;
import com.yj.suncity.bean.AdvGoodsBean;
import com.yj.suncity.constant.URLCon;

public class GoodsView extends LinearLayout{
	private Context context;
	private GoodsViewListener listener;
	private HashMap<String, SoftReference<Bitmap>> mImageCache = new HashMap<String, SoftReference<Bitmap>>();
	DisplayImageOptions options;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	
	public GoodsView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		options = new DisplayImageOptions.Builder()
		.showStubImage(R.drawable.android_layout_detail)
		.showImageForEmptyUri(R.drawable.android_layout_detail)
		.showImageOnFail(R.drawable.android_layout_detail)
		.cacheInMemory(true).cacheOnDisc(true)
		// .displayer(new RoundedBitmapDisplayer(10))// 设置成圆角图片
		.bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型
		.build();
	}
	public void setGoodsViewListener(GoodsViewListener listener){
		this.listener = listener;
	}
	public LinearLayout getLinearLayout(final AdvGoodsBean goods,final int position){
		
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();// ��Ļ���
		float scale = context.getResources().getDisplayMetrics().density;
		int scal = (int) (30 * scale + 0.5f);
		int relativeWidth =  (width - scal) / 2;
		int imageWidth = relativeWidth * 5 / 7 ;
		int buttonWidth = relativeWidth / 7;
		
		LinearLayout linearLayout = new LinearLayout(context);
		linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
		linearLayout.setOrientation(LinearLayout.HORIZONTAL);
		
		View view = LayoutInflater.from(context).inflate(R.layout.product_list_item, null);
		RelativeLayout productBgLayout = (RelativeLayout)view.findViewById(R.id.product_list_item_bg_layout);
		TextView productNameText = (TextView)view.findViewById(R.id.product_list_item_name_text);
		ImageView productImage = (ImageView)view.findViewById(R.id.product_list_item_image);
		ImageView productRight = (ImageView)view.findViewById(R.id.product_list_item_right_image);
		TextView priceText = (TextView)view.findViewById(R.id.product_list_item_price_text);
		int hight = (width - (int) (30 * scale + 0.5))/2;
		LayoutParams layoutParam = (LayoutParams) productBgLayout.getLayoutParams();
		layoutParam.height = hight;
		layoutParam.width = hight;
		productBgLayout.setLayoutParams(layoutParam);
		if(goods.getBuy_type().equals("1")){
			productRight.setImageResource(R.drawable.tejia);
		}else if(goods.getBuy_type().equals("2")){
			productRight.setImageResource(R.drawable.cuxiao);
		}else if(goods.getBuy_type().equals("3")){
			productRight.setImageResource(R.drawable.jingpin);
		}else if(goods.getBuy_type().equals("4")){
			productRight.setImageResource(R.drawable.mianfei);
		}
		
		productNameText.setText(goods.getGoods_title());
		priceText.setText("会员价："+goods.getReal_price()+"元");
		imageLoader.displayImage(
				URLCon.HOST
						+ (goods.getImg_url()),productImage, options);
		
		productBgLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				listener.itemClick(position);
			}
		});
		linearLayout.addView(view);
		return linearLayout;
	}
	public interface GoodsViewListener{
		public void itemClick(int position);
	}
	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
}
